<?php
namespace Orx\Etl\Extractor;
/**
 * Data Extractor Source Object
 *
 * It represant the data source : a file, a web site, a external software...
 * This is a standard core object of the ETL bundle, it cannot be change by 
 * anyone. 
 *
 * @author Xavier VILAIN
 * @package Orx\Etl\Extractor
 * @license #license-orx-etl#
 * 
 * @property int	$source		Source id
 * @property string	$language	Language
 * @property string	$label		Label
 * @property string	$params		List of params saved has a json file
 * 
 */
class Source extends \Fox\Object {
	
	/**
	 * Reverse Link to Extractor
	 * @var string
	 */
	const LINK_EXTRACTOR = 'extractor';
	
	/**
	 * List of all DataSource attributes, it could be 
	 * file extensions, server host name...
	 * 
	 * @var array
	 */
	private $_attributes = null;
	
	/**
	 * Json Decoded Params
	 * @var array
	 */
	private $_decodedParams = null;
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function decodeParams() {
		
		if(empty($this->_decodedParams))
		{
			/*
			$data['attributes']['extentions'] = array('dataelement' => 'file_extensions','inputtype' => 'Fox\Html\Input','mandatory' => 1);
			$data['attributes']['row_limit'] = array('dataelement' => 'file_max_nb_rows','inputtype' => 'Fox\Html\Input','mandatory' => 0);
			$data['attributes']['delimiter'] = array('dataelement' => 'file_delimiter','inputtype' => 'Fox\Html\Input','mandatory' => 0);
			*/
			$this->_decodedParams = json_decode($this->params,true);
			if(!is_array($this->_decodedParams)) {
				$this->_decodedParams= array();
			}
		}
		return $this->_decodedParams;
	}
	
	/**
	 * 
	 * Format data to be stored according to the data source attributes
	 * params.
	 * @param array $value
	 */
	public function format($value) {
		$data = null;
		$params = $this->decodeParams();
		if(isset($params['attributes'])) {
			foreach($params['attributes'] as $attributeName => $attibute) {
				$data[$attributeName] = !empty($value[$attributeName]) ? $value[$attributeName] : null;
			}
			$data = json_encode($data);
		}
		return $data;
	}
	
	/**
	 * 
	 * Parse data stored according to the data source attributes to an array
	 * to fill update screen of an extractor
	 * @param array $value
	 */
	public function parse($value) {
		$data = array();
		$params = $this->decodeParams();
		if(isset($params['attributes'])) {
			$value = json_decode($value,true);
			foreach($params['attributes'] as $attributeName => $attibute) {
				$data[$attributeName] = !empty($value[$attributeName]) ? $value[$attributeName] : null;
			}
			
		}
		return $data;
	}
	
	/**
	 * Build the attributes input block
	 * @return array of Fox\Html\Object
	 */
	public function attributes() {
		if(empty($this->_attributes)) {
			$this->_attributes = array();
			$params = $this->decodeParams();
			
			if(isset($params['attributes'])) {
				foreach($params['attributes'] as $attributeName => $attibute) {
					$inputObjectClass = $attibute['inputtype'];
					$inputObject = new $inputObjectClass(array($attibute['dataelement'],$attributeName));
					$inputObject->mandatory($attibute['mandatory']);
					$this->_attributes[$attributeName] = $inputObject;
				}
			}
		}
		return $this->_attributes;
	}
	
	/**
	 * 
	 * Source Label
	 * @todo add i18n 
	 * @return string
	 */
	public function label() {
		return $this->label;
	}
}
\Fox\Dao::infos(Source::className(),new \Fox\Object\Info('etl_extractor_sources', \Fox\Databases::DB_APP));